<?php

namespace Our;

/**
 * 控制器抽象类
 */
abstract class Controller_Abstract extends \Yaf\Controller_Abstract {

    /**
     * 输出错误的信息
     * 
     * @param string $message
     * @param int $errno 错误编号
     */
    protected function _echoErrorMessage($message, $errno = 1) {
        $return = array(
            'errno'  => $errno,
            'errmsg' => $message,
            'data'   => array()
        );
        if ($this->getRequest()->getQuery('is_jsonp')) {
            $callback = $this->getRequest()->getQuery('callback');
            if ($callback) {
                $this->getResponse()->setBody($callback . "(" . json_encode($return) . ")");
            }
        } else {
            $this->_displayJson($return);
        }
    }

    /**
     * 请求时输出成功的信息,并退出
     * 
     * @param string $message
     * @param array $data
     */
    protected function _echoSuccessMessage($message, $data = array()) {
        //检测fields字段，如果有设置则只传输相应的字段值
        $fields = array_filter(explode(',', $this->getRequest()->getParam('fields')));
        if ($fields) {
            foreach ($data as $k => $v) {
                if (!in_array($k, $fields)) {
                    unset($data[$k]);
                }
            }
        }

        $return = array(
            'errno'  => "0",
            'errmsg' => $message,
            'data'   => $data
        );

        //检测是否有jsonp的
        if ($this->getRequest()->getQuery('is_jsonp')) {
            $callback = $this->getRequest()->getQuery('callback');
            if ($callback) {
                $this->getResponse()->setBody($callback . "(" . json_encode($return) . ")");
            }
        } else {
            $this->_displayJson($return);
        }
    }

    /**
     * 输出json格式的数据
     * 
     * @param mixed $data
     */
    public function _displayJson($data) {
        \Yaf\Dispatcher::getInstance()->disableView();
        if (!$this->getRequest()->isCli()) {
            header('Content-Type: application/json; charset=utf-8');
        }
        $this->getResponse()->setBody(json_encode($data));
    }

    /**
     * 获取post的参数
     * 
     * @param string $key
     * @param mix $defaultValue
     * @return mix
     */
    protected function _getPost($key = null, $defaultValue = null) {
        if (!$key) {
            return $_POST;
        }

        $value = $this->getRequest()->getPost($key);
        if (!$value) {
            $value = isset($_POST[$key]) ? $_POST[$key] : null;
        }
        if (!$value) {
            $value = $defaultValue;
        }
        return $value;
    }

    /**
     * 获取get参数
     * 
     * @param type $key
     * @param type $defaultValue
     * @return type
     */
    protected function _getParam($key = null, $defaultValue = null) {
        if (!$key) {
            $params = $this->getRequest()->getParams();
            if ($defaultValue) {
                foreach ($defaultValue as $k => $v) {
                    if (empty($params[$k])) {
                        $params[$k] = $v;
                    }
                }
            }

            return $params;
        }

        return $this->getRequest()->getParam($key, $defaultValue);
    }

    /**
     * 
     * @param type $key
     * @param type $defaultValue
     * @return type
     */
    protected function _getQuery($key = null, $defaultValue = null) {
        if (!$key) {
            $params = $this->getRequest()->getQuery();
            if ($defaultValue) {
                foreach ($defaultValue as $k => $v) {
                    if (empty($params[$k])) {
                        $params[$k] = $v;
                    }
                }
            }

            return $params;
        }

        return $this->getRequest()->getQuery($key, $defaultValue);
    }

}
